/**
 * Simple inline style renderer.
 * 
 * @author Andrey Anisimov <hercules@inbox.ru>
 */

class Style {
	
	/**
	 * Creates new inline style renderer.
	 *
	 * @param props (object) [optional] default properties.
	 */
	this.Style = function(props) {
		this.props = props || {};
		this.filters = {};
	}
	
	/**
	 * Sets the property value.
	 *
	 * @param name (string) property name
	 * @param value (string) property value
	 */
	this.setProperty = function(name, value) {
		this.props[name] = value;
	}
	
	/**
	 * Returns property value.
	 *
	 * @param name (string) property name
	 * @return string
	 */
	this.getProperty = function(name) {
		return this.props[name];
	}
	
	/**
	 * Gets whether the style has the specified property.
	 *
	 * @return bool
	 */
	this.hasProperty = function(name) {
		return (this.props[name] !== undefined);
	}
	
	/**
	 * Removes style property.
	 *
	 * @param name (string) property name
	 */
	this.removeProperty = function(name) {
		delete this.props[name];
	}
	
	/**
	 * Sets the style filter.
	 *
	 * @param name (string) filter name (e.g. Alpha)
	 * @param values (object) [optional] filter values
	 */
	this.setFilter = function(name, values) {
		this.filters[name] = values || {};
	}
	
	/**
	 * Gets whether the style has the specified filter.
	 *
	 * @param name (string) filter name
	 * @return bool
	 */
	this.hasFilter = function(name) {
		return (this.filters[name] !== undefined);
	}
	
	/**
	 * Removes style filter.
	 *
	 * @param name (string) filter name
	 */
	this.removeFilter = function(name) {
		delete this.filters[name];
	}
	
	/**
	 * Sets the opacity value.
	 *
	 * @param o (int) opacity (from 0 to 100)
	 */
	this.setOpacity = function(o) {
		if (100 == o) {
			this.removeProperty('opacity');
			this.removeProperty('-moz-opacity');
			this.removeProperty('-khtml-opacity');
			this.removeFilter('Alpha');
		} else {
			this.setProperty('opacity', o / 100);
			this.setProperty('-moz-opacity', o / 100);
			this.setProperty('-khtml-opacity', o / 100);
			this.setFilter('Alpha' , { 'opacity' : o });
		}
	}
	
	/**
	 * Returns current opacity value (from 0 to 100).
	 *
	 * @return int
	 */
	this.getOpacity = function() {
		return this.hasProperty('opacity') ? this.getProperty('opacity') : 100;
	}
	
	/**
	 * Generates CSS string.
	 *
	 * @return string
	 */
	this.toString = function() {
		
		var parts = [];
		for (var name in this.props) if (this.props[name]) {
			parts.push(name + ': ' + this.props[name]);
		}
		
		var filterParts = [];
		for (var name in this.filters) {
			var values = this.filters[name];
			var valuesParts = [];
			if ('object' == typeof values) {
				for (var k in values) {
					valuesParts.push(k + '=' + values[k]);
				}
			}
			filterParts.push('filter:progid:DXImageTransform.Microsoft.' + name + '(' + valuesParts.join(', ') + ')');
		}
		
		if (filterParts.length > 0) {
			parts.push(filterParts.join(' '));
		}
		
		return parts.join('; ');
	}
}